package com.ctrip.framework.apollo.openapi.server.service;

import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceLockDTO;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.openapi.api.NamespaceOpenApiService;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceLockDTO;
import com.ctrip.framework.apollo.openapi.util.OpenApiBeanUtils;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.service.AppNamespaceService;
import com.ctrip.framework.apollo.portal.service.NamespaceLockService;
import com.ctrip.framework.apollo.portal.service.NamespaceService;

import java.util.List;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
public class ServerNamespaceOpenApiService implements NamespaceOpenApiService {

    private final AppNamespaceService appNamespaceService;
    private final ApplicationEventPublisher publisher;
    private final NamespaceService namespaceService;
    private final NamespaceLockService namespaceLockService;

    public ServerNamespaceOpenApiService(AppNamespaceService appNamespaceService, ApplicationEventPublisher publisher, NamespaceService namespaceService, NamespaceLockService namespaceLockService) {
        this.appNamespaceService = appNamespaceService;
        this.publisher = publisher;
        this.namespaceService = namespaceService;
        this.namespaceLockService = namespaceLockService;
    }

    @Override
    public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName, String namespaceName) {
        NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName);
        if (namespaceBO == null) {
            return null;
        }
        return OpenApiBeanUtils.transformFromNamespaceBO(namespaceBO);
    }

    @Override
    public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
        return OpenApiBeanUtils.batchTransformFromNamespaceBOs(namespaceService.findNamespaceBOs(appId, Env.valueOf(env), clusterName));
    }

    @Override
    public OpenAppNamespaceDTO createAppNamespace(OpenAppNamespaceDTO appNamespaceDTO) {
        AppNamespace appNamespace = OpenApiBeanUtils.transformToAppNamespace(appNamespaceDTO);
        AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace, appNamespaceDTO.isAppendNamespacePrefix());

        publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));

        return OpenApiBeanUtils.transformToOpenAppNamespaceDTO(createdAppNamespace);
    }

    @Override
    public OpenNamespaceLockDTO getNamespaceLock(String appId, String env, String clusterName, String namespaceName) {
        NamespaceDTO namespace = namespaceService.loadNamespaceBaseInfo(appId, Env.valueOf(env), clusterName, namespaceName);
        NamespaceLockDTO lockDTO = namespaceLockService.getNamespaceLock(appId, Env.valueOf(env), clusterName, namespaceName);
        return OpenApiBeanUtils.transformFromNamespaceLockDTO(namespace.getNamespaceName(), lockDTO);
    }
}
